/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.scalia.meteocal.gui;

import com.scalia.meteocal.control.EventManager;
import com.scalia.meteocal.control.InvitationManager;
import com.scalia.meteocal.entity.Event;
import com.scalia.meteocal.entity.Invitation;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultScheduleEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleEvent;
import org.primefaces.model.ScheduleModel;
import org.primefaces.model.menu.DefaultMenuItem;
import org.primefaces.model.menu.DefaultMenuModel;
import org.primefaces.model.menu.DefaultSubMenu;
import org.primefaces.model.menu.MenuModel;

/**
 *
 * @author gabriele
 */
@ManagedBean
@ViewScoped
public class UserPageBean implements Serializable {
    
    private class MeteocalScheduleEvent extends DefaultScheduleEvent {
        private Long eventID;
        public MeteocalScheduleEvent(Long eventID, String title, Date start, Date end, boolean isCreator) {
            super(title, start, end, isCreator ? "creator" : "not_creator");
            this.eventID = eventID;
        }
        public Long getEventID() {
            return eventID;
        }
        public void setEventID(Long eventID) {
            this.eventID = eventID;
        }
    }
    
    @EJB
    private EventManager eventManager;
  
    @EJB
    private InvitationManager invitationManager;
    
    private List<Invitation> myInvitations;
    private ScheduleModel model;
    private MenuModel menuModel;
    
    @PostConstruct
    public void init() {
        menuModel = new DefaultMenuModel();

        List<Event> myEvents = eventManager.getAllMyEvents();
        myInvitations = invitationManager.getMyInvitations();

        DefaultSubMenu myEventsSubMenu = new DefaultSubMenu("My events");
        DefaultMenuItem item;

        for (Event e : myEvents) {
            item = new DefaultMenuItem(e.getName());
            item.setIcon("ui-icon-disk");
            item.setUrl("/user/eventpage.xhtml?id="+e.getId());            
            myEventsSubMenu.addElement(item);
        }

        menuModel.addElement(myEventsSubMenu);
        
        //CALENDAR SETUP
        model = new DefaultScheduleModel();
        myEvents.stream().forEach((e) -> {
            addEvent(e, true);
        });
        
        List<Event> myPartecipations = eventManager.getAllMyPartecipations();
        for (Event e : myPartecipations) {
            addEvent(e, false);
        }
    }

    public MenuModel getMenuModel() {
        return menuModel;
    }
    
    public List<Invitation> getMyInvitations() {
        return myInvitations;
    }
    
    public void changeInvitation() {
        
        FacesContext context = FacesContext.getCurrentInstance();
        Invitation invitation = context.getApplication().evaluateExpressionGet(context, "#{invitation}", Invitation.class);
        invitationManager.changeInvitation(invitation);
       
        
        //CALENDAR MANAGEMENT
        Event e = invitation.getEvent();
        if (invitation.isAccepted()) {
            addEvent(e, false);
        } else {
            Long eventID = e.getId();
            for (ScheduleEvent scheduleEvent : model.getEvents()) {
                MeteocalScheduleEvent meteocalScheduleEvent = (MeteocalScheduleEvent)scheduleEvent;
                if (meteocalScheduleEvent.getEventID() == eventID) {
                    model.deleteEvent(scheduleEvent);
                    break;
                }
            }
        }
        
    }
    
    private void addEvent(Event e, boolean isCreator) {
        MeteocalScheduleEvent scheduleEvent = new MeteocalScheduleEvent(e.getId(), e.getName(), e.getStartTime(), e.getEndTime(), isCreator);
        scheduleEvent.setDescription("Event " + e.getName() + " organized by " + e.getCreator().getUsername() + " in the place " + e.getPlace() + " at " + e.getStartTime());
        model.addEvent(scheduleEvent);
    }
     
    public ScheduleModel getModel() {
        return model;
    }
    
    public void onEventSelect(SelectEvent selectEvent) {
        MeteocalScheduleEvent event = (MeteocalScheduleEvent) selectEvent.getObject();
        long eventID = event.getEventID();
        
        RequestContext.getCurrentInstance().execute("window.open('/meteocal/user/eventpage.xhtml?id=" + eventID + "' ,'_self')");
    }
}
